package service_sys

import (
	"os"
	"strings"
	"wrblog-api-go/app/dao/dao_sys"
	"wrblog-api-go/app/model/model_sys"
)

func GetList(sysFile model_sys.SelectSysFile) ([]model_sys.SysFilePo, int64, error) {
	return dao_sys.GetList(sysFile)
}

func GetListIdAll(sysFile model_sys.SelectSysFile) ([]string, error) {
	pageSize := -1
	sysFile.PageSize = &pageSize
	sysFiles, _, err := GetList(sysFile)
	if err != nil {
		return nil, err
	}
	var fileIds []string
	for _, item := range sysFiles {
		fileIds = append(fileIds, item.FileId)
	}
	return fileIds, nil
}

func AddFile(sysFile model_sys.SysFilePo) (model_sys.SysFilePo, error) {
	return dao_sys.AddFile(sysFile)
}

func GetFileById(fileId string) (model_sys.SysFilePo, error) {
	return dao_sys.GetFileById(fileId)
}

func GetFileByIds(fileIds string) ([]model_sys.SysFilePo, error) {
	return dao_sys.GetFileByIds(strings.Split(fileIds, ","))
}

func EditFile(sysFile model_sys.SysFilePo) (int64, error) {
	return dao_sys.EditFile(sysFile)
}

func RemoveSysFile(fileIds string) (int64, error) {
	sysFiles, err := GetFileByIds(fileIds)
	if err != nil {
		return 0, err
	}
	go func() {
		for _, sysFile := range sysFiles {
			os.Remove(sysFile.AbsolutePath)
		}
	}()
	return dao_sys.RemoveSysFile(strings.Split(fileIds, ","))
}
